package com.liam.design.function;


import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.*;


public class HelloFunction {

    static List<Book> books = new ArrayList<>();

    static {
        Book book1 = Book.builder().title("葵花宝典").pages(13).author("A").build();
        Book book2 = Book.builder().title("葵花宝典2").pages(12).author("A").build();
        Book book3 = Book.builder().title("葵花宝典3").pages(6).author("A").build();
        Book book4 = Book.builder().title("葵花宝典4").pages(1).author("A4").build();
        books.add(book1);
        books.add(book2);
        books.add(book3);
        books.add(book4);
    }


    public static void main(String[] args) {
//        sort();
//        getBook();
//        random();
        op();
    }

    /**
     * 操作符
     */
    public static void op() {
        // 排序
        books.stream().sorted(Comparator.comparing(Book::getPages))
                .forEachOrdered(book -> System.out.println(book.getPages()));

        // 取第一匹配的数据
        books.stream().filter(book -> book.getTitle().equals("葵花宝典"))
                .findFirst();

        // list中是否存在某个
        boolean flag = books.stream().anyMatch(book -> "葵花宝典".equals(book.getTitle()));

        //collect 转map
        books.stream().collect(Collectors.toMap(
                Book::getAuthor,
                Book::getPages,
                // 已存在就按已存在老的为准
                (existing,replace) -> existing
        ));


        // 求和
        int sum =  books.stream().collect(summingInt(Book::getPages));
        System.out.println(sum);

        // 平均数
        Double ave = books.stream().collect(averagingDouble(Book::getPages));
        System.out.println(ave);

        // 所有计算都有，平均数，最大，最小，求合
        DoubleSummaryStatistics statistics = books.stream().collect(summarizingDouble(Book::getPages));
        System.out.println(statistics.getMax()+","+ statistics.getMin());


        // 还有count，max ，map（转其他任何对象）
    }


    /**
     * 随机
     */
    public static void random() {
        List<Double> list = Stream.generate(() -> Math.random())
                .limit(10)
                .peek(System.out::println)
                .collect(Collectors.toList());

    }


    /**
     * 页数最多
     * 前3条
     * 除了书名为葵花宝典
     * 作者的名字
     */
    public static void getBook() {


        books.stream().filter(book -> !"葵花宝典".equals(book.getTitle()))// 除了书名为葵花宝典
                .sorted((b1, b2) -> b1.getPages().compareTo(b2.getPages()))//页数最多
                .limit(3)//前3条
                .map(Book::getAuthor)//作者的名字
                .distinct()// 去重复
                .forEach(System.out::println);

    }

    /**
     * 排序
     */
    public static void sort() {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(8);
        list.add(3);

        Collections.sort(list, (o1, o2) -> o2.compareTo(o1));
        list.stream().forEach(i -> System.out.println(i));
    }
}
